/**
 * @file 页面适配相关API
 */

/**
 * rem适配用，设置html字体大小，css单位使用rem做自适应
 * @param portPixel 竖屏时宽度，默认为750
 * @param landPixel 横屏时宽度，默认为1334
 */
export const remAdapt = (portPixel: number = 750, landPixel: number = 1334) => {
  if (!window.addEventListener) return
  function setFont() {
    var html = document.documentElement
    var k = portPixel
    var w = document.documentElement.clientWidth,
      h = document.documentElement.clientHeight
    if (w > h) {
      k = landPixel
      document.documentElement.setAttribute('class', 'landscape')
    }
    if (w < h) {
      k = portPixel
      document.documentElement.setAttribute('class', 'portrait')
    }
    html.style.fontSize = (html.clientWidth / k) * 100 + 'px'
  }

  setFont()
  setTimeout(function () {
    setFont()
  }, 300)
  document.addEventListener('DOMContentLoaded', setFont, false)
  window.addEventListener('orientationchange', setFont, false)
  window.addEventListener('resize', setFont, false)
  window.addEventListener('load', setFont, false)
}
